package com.hontech.kaltaapp.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.TextView
import com.hontech.kaltaapp.R

class LabelInput(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val et: EditText
    private val key: String

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_label_input, this)
        val tv = view.findViewById<TextView>(R.id.id_input_tv)
        et = view.findViewById(R.id.id_id_input_et)

        val attr = context.obtainStyledAttributes(attrs, R.styleable.LabelInput)
        key = attr.getString(R.styleable.LabelInput_li_key)!!
        val def = attr.getString(R.styleable.LabelInput_li_def) ?: "0"
        et.setText(def)
        attr.recycle()

        tv.text = key
    }

    fun set(v: Int) {
        et.setText(v.toString())
    }

    fun textInt(): Int {
        val s = et.text.toString()
        if (s.isEmpty()) {
            throw IllegalStateException("请输入:$key")
        }
        return s.toInt()
    }
}

